<?php

declare (strict_types=1);
/**
 * @copyright 深圳市易果网络科技有限公司
 * @version 1.0.0
 * @link https://dayiguo.com
 */

namespace App\Service\User;

use App\Constants\Constants;
use App\Kernel\Util\UserJwtInstance;
use App\Service\Service;

use Hyperf\Di\Annotation\Inject;

/**
 * 支付宝授权服务
 *
 * @author 张昊(2396675557@qq.com)
 * @package App\Service\Admin
 */
class AliService extends Service
{

    /**
     * 支付宝授权
     * @param string $code
     * @return mixed
     */
    public function Auth(string $code)
    {

        $aop = (new \App\Kernel\Ali\AopClient($this->guzzle));

        //网关，正式环境需要将alipaydev改成alipay
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        //appId
        $aop->appId = '2021001135615913';
        //开发者私钥
        $aop->rsaPrivateKey = config('alipay.auth_private_key');
        //支付宝公钥
        $aop->alipayrsaPublicKey = config('alipay.auth_public_key');
        //签名算法类型
        $aop->signType = 'RSA2';

        $request = (new \App\Kernel\Ali\AlipaySystemOauthTokenRequest());
        $request->setGrantType("authorization_code");
        $request->setCode($code);
        $result = $aop->execute($request);

        $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
        //获取access_token
        $return = $result->$responseNode ?? [];
        return $return;
    }
}